const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  
  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

/**
 * 超链接跳转
 */
function handleHref(content) {
  if (!content) {
    return false;
  }
  try {
    if (content.indexOf('http://') == 0 || content.indexOf('https://') == 0) { //超链接跳转
      wx.navigateTo({
        url: '/pages/web/web?url=' + content,
      })
      return true;
    } else if (content.indexOf('good://') == 0) { //跳转商品详情
      var data = content.substring(7, content.length)
      wx.navigateTo({
        url: '/pages/goods/goods-detail/goods-detail?' + data,
      })
      return true;
    } else if (content.indexOf("tel://") == 0) { //拨打电话
      var data = content.substring(6, content.length)
      wx.makePhoneCall({
        phoneNumber: data,
      })
      return true;
    } else if (content.indexOf("order://") == 0) { //订单详情
      var data = content.substring(8, content.length)
      wx.navigateTo({
        url: '/pages/order/order-detail/order-detail?' + data,
      })
      return true;
    }
  } catch (e) {}

  return false
}

function handleShareApp(page, res) {
  if (page=="shop") {
    return {
      title: res.mchName || '',
      path: '/pages/splash/splash?page=' + page + "&mchid=" + res.mchId,
      imageUrl: res.mchLogoUrl || ''
    }
  } else if (page =="shop-details"){
    return {
      title: res.name || '',
      path: '/pages/splash/splash?page=' + page + "&mchid=" + res.mchId+"&id="+res.id,
      imageUrl: res.imageUrl.split(",")[0] || ''
    }
  } else {
    return {
      title: '',
      path: '/pages/splash/splash',
      imageUrl: ''
    }
  }

}

module.exports = {
  formatTime: formatTime,
  handleHref: handleHref,
  handleShareApp: handleShareApp
}